package com.jxzyk.model.center;

import org.json.JSONException;

import com.alibaba.fastjson.JSONObject;
import com.core.ui.activity.BaseActivity;
import com.core.util.UiTools;
import com.jxzyk.R;
import com.jxzyk.config.API;
import com.jxzyk.entity.User;
import com.jxzyk.http.BaseRequest.OnDialogListener;
import com.jxzyk.http.req.PersonDataRequest;
import com.jxzyk.util.UserManger;
import com.jxzyk.view.TitleLayout;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

@ContentView(R.layout.act_persondata)
public class PersonDataActivity extends BaseActivity {
	User user;
	String name, sid, major, age, sex;
	UserManger manager;

	@Override
	public void initData(Bundle bundle) {
		// TODO Auto-generated method stub

	}

	@ViewInject(R.id.title)
	TitleLayout title;
	@ViewInject(R.id.et_persondata_name)
	EditText et_name;
	@ViewInject(R.id.et_persondata_SID)
	EditText et_sid;
	@ViewInject(R.id.et_persondata_major)
	EditText et_major;
	@ViewInject(R.id.et_persondata_age)
	EditText et_age;
	@ViewInject(R.id.rb_man)
	RadioButton rb_man;
	@ViewInject(R.id.rb_woman)
	RadioButton rb_woman;
	@ViewInject(R.id.rg_persondata)
	RadioGroup group;

	@Override
	public void initUi() {
		// TODO Auto-generated method stub
		title.setTtile("个人资料");
		initView();
		manager = new UserManger(this);
		try {
			user = manager.getUser();
			showData();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void initView() {
		// TODO Auto-generated method stub
		title.setOnLeftListener(R.drawable.ic_chevron_left_white_48dp, new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		title.setOnRightListener("完成", new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				saveData();
			}
		});
	}

	protected void showData() {
		et_name.setText(user.getUser_name());
		et_sid.setText(user.getUser_xh());
		et_major.setText(user.getUser_zy());
		et_age.setText(user.getUser_age());
		if (user.getUser_sex().equals("男"))
			rb_man.setChecked(true);
		else
			rb_woman.setChecked(true);
	}

	protected void saveData() {
		// TODO Auto-generated method stub
		name = et_name.getText().toString();
		sid = et_sid.getText().toString();
		major = et_major.getText().toString();
		age = et_age.getText().toString();
		if (group.getCheckedRadioButtonId() == R.id.rb_man)
			sex = "男";
		else
			sex = "女";
		if (UiTools.Empty(name, sid, major, age, sex)) {
			PersonDataRequest request = new PersonDataRequest(this, API.personCenter.persondata);
			request.setParams(name, sid, major, sex, age, user.getUser_id());
			request.setOnDialogListener(new OnDialogListener() {

				@Override
				public void onSuccess(JSONObject json) {
					// TODO Auto-generated method stub
					User muser = new User();
					muser = user;
					muser.setUser_name(name);
					muser.setUser_age(age);
					muser.setUser_xh(sid);
					muser.setUser_sex(sex);
					muser.setUser_zy(major);
					try {
						manager.updateUser(muser);
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					finish();
				}

				@Override
				public void onFailure(String msg) {
					// TODO Auto-generated method stub
					Toast.makeText(getBaseContext(), "保存失败！请检查网络", Toast.LENGTH_LONG).show();
				}
			});
			request.show();
		} else
			Toast.makeText(this, "输入不能为空！", Toast.LENGTH_LONG).show();

	}

}
